# -*- coding:utf-8 -*-
from cassandra.cluster import Cluster
from cassandra import ConsistencyLevel
from datetime import *
import json
import os

from wmcloud_dataclient import Client

DEFAULT_DEDATE = '1970-01-01'
def get_fundation_basic_info(raw_msg):
    try:
        foundation_basic_info = json.loads(raw_msg)
        if foundation_basic_info['retCode'] == 1 and foundation_basic_info['retMsg'] == 'Success':
            foundation_info_d_list = foundation_basic_info['data']
            cluster = Cluster(protocol_version=4)
            session = cluster.connect()
            session.set_keyspace('wmcloud')
            insert_found = session.prepare('insert into wmcloud.foundation_basic_info(ticker, establishdate, circulationshares, liststatuscd, listdate, delistdate, category, operationmode, indexfund, etflof, custodian, custodianfullname, exchangecd, guarperiod, guarratio, investfield, investtarget, isclass, isfof, isguarfund, isqdii, managementcompany, managementfullname, managername, perfbenchmark, secid, secshortname, tradeabbrname) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')
            for data in foundation_info_d_list:
                establishdate_timestamp = datetime.strptime(data['establishDate'], '%Y-%m-%d') + timedelta(hours=16) if 'establishDate' in data else datetime.strptime(DEFAULT_DEDATE, '%Y-%m-%d')
                listdate_timestamp = datetime.strptime(data['listDate'], '%Y-%m-%d') + timedelta(hours=16) if 'listDate' in data else datetime.strptime(DEFAULT_DEDATE, '%Y-%m-%d')
                delistdate_timestamp = datetime.strptime(data['delistDate'], '%Y-%m-%d') + timedelta(hours=16) if 'delistDate' in data else datetime.strptime(DEFAULT_DEDATE, '%Y-%m-%d')
                print(data['ticker'])
                session.execute(insert_found, (data['ticker'], establishdate_timestamp, data['circulationShares'] if 'circulationShares' in data else 0.0, data['listStatusCd'], listdate_timestamp, delistdate_timestamp, data['category'], data['operationMode'], data['indexFund'] if 'indexFund' in data else 'unknown', data['etfLof'] if 'etfLof' in data else 'unknown', data['custodian'], data['custodianFullName'] if 'custodianFullName' in data else 'unknown', data['exchangeCd'] if 'exchangeCd' in data else 'unknown', data['guarPeriod'] if 'guarPeriod' in data else 0.0, data['guarRatio'] if 'guarRatio' in data else 0.0, data['investField'] if 'investField' in data else 'unknown', data['investTarget'] if 'investTarget' in data else 'unknown', data['isClass'] if 'isClass' in data else 0, data['isFof'] if 'isFof' in data else False, data['isGuarFund'] if 'isGuarFund' in data else False, data['isQdii'] if 'isQdii' in data else False, data['managementCompany'], data['managementFullName'] if 'managementFullName' in data else 'unknown', data['managerName'] if 'managerName' in data else 'unknown', data['perfBenchmark'] if 'perfBenchmark' in data else 'unknown', data['secID'], data['secShortName'], data['tradeAbbrName'] if 'tradeAbbrName' in data else 'unknown'))
            session.shutdown()
            cluster.shutdown()
    except Exception as e:
        print(e)

if __name__ == '__main__':
    client = Client('93ef5ebba60c5f1da190801750612423db818daad66155b6b191417b19d331b9')
    url = '/api/fund/getFund.json?field=&ticker=&secID=&etfLof=&listStatusCd=L,S,DE,UN'
    try:
        code, result = client.getData(url)
        if code == 200:
            get_fundation_basic_info(result)
    except Exception as e:
        print(e)
